package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceFeedbackListConfirmedAdapter;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceFeedbackListWaitConfirmAdapter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartFeedbackResponse;
import com.cloudsigns.factorymanager2.produce.iview.IProductFeedbackListView;
import com.cloudsigns.factorymanager2.produce.presenter.ProductFeedbackListPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

public class ProduceFeedbackListActivity extends BaseActivity implements IProductFeedbackListView {
    private RelativeLayout rl_back, rl_right;
    private TextView tv_title;
    private View rl_progressbar;
    private ListViewForScrollView lv_wait, lv_confirmed;

    private ProduceFeedbackListWaitConfirmAdapter waitAdapter;
    private ProduceFeedbackListConfirmedAdapter confirmedAdapter;

    private ProductFeedbackListPresenter productFeedbackListPresenter;

    List<WorkPartFeedbackResponse> waitList = new ArrayList<>();
    List<WorkPartFeedbackResponse> confirmedList = new ArrayList<>();

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_feedback);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        rl_right = findViewById(R.id.rl_right);
        tv_title = findViewById(R.id.tv_title);
        rl_progressbar = findViewById(R.id.rl_progressbar);
        lv_wait = findViewById(R.id.lv_wait);
        lv_confirmed = findViewById(R.id.lv_confirmed);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.feedback_list));

        waitAdapter = new ProduceFeedbackListWaitConfirmAdapter(this);
        confirmedAdapter = new ProduceFeedbackListConfirmedAdapter(this);

        productFeedbackListPresenter = new ProductFeedbackListPresenter(this);
        productFeedbackListPresenter.productFeedbackList();

    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);

        lv_wait.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                WorkPartFeedbackResponse workPartFeedbackResponse = waitList.get(position);
                Intent mIntent = new Intent(ProduceFeedbackListActivity.this, ProduceFeedbackDetailsActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, workPartFeedbackResponse);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
            }
        });

        lv_confirmed.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                WorkPartFeedbackResponse workPartFeedbackResponse = confirmedList.get(position);
                Intent mIntent = new Intent(ProduceFeedbackListActivity.this, ProduceFeedbackDetailsActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, workPartFeedbackResponse);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
            }
        });
    }

    @Override
    public void widgetClick(View v) {
        Intent mIntent;
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.rl_right:
                mIntent = new Intent(ProduceFeedbackListActivity.this, ProduceEditFeedbackActivity.class);
                startActivity(mIntent);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    productFeedbackListPresenter.productFeedbackList();
                    waitAdapter.notifyDataSetChanged();
                    confirmedAdapter.notifyDataSetChanged();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void getProductFeedbackListSuccessed(List<WorkPartFeedbackResponse> workPartFeedbackResponseList) {
        if (null != workPartFeedbackResponseList) {
            waitList.clear();
            confirmedList.clear();
            for (WorkPartFeedbackResponse item : workPartFeedbackResponseList) {
                if (item.getStatus() == CommonEnum.FeedbackStatus.WAIT_CONFIRM || item.getStatus() == CommonEnum.FeedbackStatus.WAIT_REPLY_CONFIRM) {
                    waitList.add(item);
                } else if (item.getStatus() == CommonEnum.FeedbackStatus.CONFIRM_FINISH || item.getStatus() == CommonEnum.FeedbackStatus.REPLY_CONFIRM_FINISH) {
                    confirmedList.add(item);
                }
            }

            waitAdapter.setData(waitList);
            lv_wait.setAdapter(waitAdapter);

            confirmedAdapter.setData(confirmedList);
            lv_confirmed.setAdapter(confirmedAdapter);

            waitAdapter.notifyDataSetChanged();
            confirmedAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showProductFeedbackListFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {
        rl_progressbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        rl_progressbar.setVisibility(View.GONE);
    }
}
